package com.seanliao.nav.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.seanliao.nav.common.domain.dto.ProjectAppDTO;
import com.seanliao.nav.common.domain.dto.valid.Add;
import com.seanliao.nav.common.domain.dto.valid.Edit;
import com.seanliao.nav.common.exception.BusinessException;
import com.seanliao.nav.common.response.Result;
import com.seanliao.nav.common.response.ResultBuilder;
import com.seanliao.nav.entity.ProjectApp;
import com.seanliao.nav.service.IProjectAppService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 项目应用管理
 */
@RestController
@RequestMapping("/app")
@RequiredArgsConstructor
public class ProjectAppController {

    private final IProjectAppService projectAppService;

    /**
     * 获取所有应用
     */
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('app:list')")
    public Result<List<ProjectApp>> list(@RequestParam(required = false) String search) {
        LambdaQueryWrapper<ProjectApp> lqw = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(search)) {
            lqw.and(lqwT -> lqwT
                    .or().like(ProjectApp::getName, search)
                    .or().like(ProjectApp::getBelongServerIp, search)
            );
        }
        List<ProjectApp> list = projectAppService.list(lqw);
        return ResultBuilder.success(list);
    }

    /**
     * 添加应用
     */
    @PostMapping("/add")
    @PreAuthorize("@pms.hasPermission('app:add')")
    public Result<?> add(@Validated(Add.class) @RequestBody ProjectAppDTO projectAppDTO) {
        // 检查应用名重复
        Assert.isFalse(projectAppService.count(new LambdaQueryWrapper<ProjectApp>()
                .eq(ProjectApp::getName, projectAppDTO.getName())
                .eq(ProjectApp::getIsDeleted, false)
        ) > 0, () -> new BusinessException("应用名已存在"));
        // 实体
        ProjectApp projectApp = BeanUtil.copyProperties(projectAppDTO, ProjectApp.class);
        if (projectApp.getId() == 0L) {
            projectApp.setId(null);
        }
        if (projectAppService.save(projectApp)) {
            return ResultBuilder.success();
        } else {
            return ResultBuilder.fail("数据写库异常");
        }
    }

    /**
     * 添加应用
     */
    @PostMapping("/edit")
    @PreAuthorize("@pms.hasPermission('app:edit')")
    public Result<?> edit(@Validated(Edit.class) @RequestBody ProjectAppDTO projectAppDTO) {
        // 检查应用名重复
        Assert.isFalse(projectAppService.count(new LambdaQueryWrapper<ProjectApp>()
                .eq(ProjectApp::getName, projectAppDTO.getName())
                .ne(ProjectApp::getId, projectAppDTO.getId())
                .eq(ProjectApp::getIsDeleted, false)
        ) > 0, () -> new BusinessException("应用名已存在"));
        // 实体
        ProjectApp projectApp = BeanUtil.copyProperties(projectAppDTO, ProjectApp.class);
        if (projectAppService.updateById(projectApp)) {
            return ResultBuilder.success();
        } else {
            return ResultBuilder.fail("数据写库异常");
        }
    }

    /**
     * 删除应用
     */
    @PostMapping("/remove")
    @PreAuthorize("@pms.hasPermission('app:remove')")
    public Result<?> remove(@RequestParam Long appId) {
        if (projectAppService.removeById(appId)) {
            return ResultBuilder.success();
        } else {
            return ResultBuilder.fail("数据写库异常");
        }
    }

}
